//
//  UIButton+YYAdd.m
//  YYKit
//
//  Created by championfu on 2024/11/26.
//  Copyright © 2024 ibireme. All rights reserved.
//

#import "UIButton+YYAdd.h"
#import <objc/runtime.h>

@implementation UIButton (YYAdd)

/// 设置图文混排，默认图文边界间距5px
/// @param layoutStyle 图文混排样式
/// @param padding 图文间距
- (void)contentLayout:(YYButtonContentLayoutStyle)layoutStyle padding:(CGFloat)padding{
    [self contentLayout:layoutStyle padding:padding margin:5];
}

/// 设置图文混排
/// @param layoutStyle 图文混排样式
/// @param padding 图文间距
/// @param margin 图文边界的间距
- (void)contentLayout:(YYButtonContentLayoutStyle)layoutStyle
                 padding:(CGFloat)padding
               margin:(CGFloat)margin{
    self.layoutType = layoutStyle;
    self.padding = padding;
    self.margin = margin;
    self.yyLayoutFlag = YES;
    [self setNeedsLayout];
}

+ (void)initialize {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method newMethod = class_getInstanceMethod([self class], @selector(yy_layoutSubviews));
        Method method = class_getInstanceMethod([self class], @selector(layoutSubviews));
        method_exchangeImplementations(newMethod, method);
    });
}

- (void)yy_layoutSubviews {
    [self yy_layoutSubviews];
    if (!self.yyLayoutFlag) return;
    YYButtonContentLayoutStyle layoutStyle = self.layoutType;
    CGFloat padding = self.padding;
    CGFloat margin = self.margin;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    CGFloat imageWith   = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    CGFloat labelWidth = 0, labelHeight = 0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        labelWidth  = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
        CGSize size = [self.titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        labelWidth = size.width;
        labelHeight = size.height;
    } else {
        labelWidth  = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    UIEdgeInsets imageEdge = UIEdgeInsetsZero;
    UIEdgeInsets titleEdge = UIEdgeInsetsZero;
    switch (layoutStyle) {
        case YYButtonContentLayoutStyleNormal:{
            titleEdge = UIEdgeInsetsMake(0, padding, 0, 0);
            imageEdge = UIEdgeInsetsMake(0, 0, 0, padding);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        } break;
        case YYButtonContentLayoutStyleCenterImageRight:{
            titleEdge = UIEdgeInsetsMake(0, -imageWith - padding, 0, imageWith);
            imageEdge = UIEdgeInsetsMake(0, labelWidth + padding, 0, -labelWidth);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        } break;
        case YYButtonContentLayoutStyleCenterImageTop:{
            titleEdge = UIEdgeInsetsMake(0, -imageWith, -imageHeight - padding, 0);
            imageEdge = UIEdgeInsetsMake(-labelHeight - padding, 0, 0, -labelWidth);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        } break;
        case YYButtonContentLayoutStyleCenterImageBottom:{
            titleEdge = UIEdgeInsetsMake(-imageHeight - padding, -imageWith, 0, 0);
            imageEdge = UIEdgeInsetsMake(0, 0, -labelHeight - padding, -labelWidth);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        } break;
        case YYButtonContentLayoutStyleLeftImageLeft:{
            titleEdge = UIEdgeInsetsMake(0, padding + margin, 0, 0);
            imageEdge = UIEdgeInsetsMake(0, margin, 0, 0);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        } break;
        case YYButtonContentLayoutStyleLeftImageRight:{
            titleEdge = UIEdgeInsetsMake(0, -imageWith + margin, 0, 0);
            imageEdge = UIEdgeInsetsMake(0, labelWidth + padding + margin, 0, 0);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        } break;
        case YYButtonContentLayoutStyleRightImageLeft:{
            imageEdge = UIEdgeInsetsMake(0, 0, 0, padding + margin);
            titleEdge = UIEdgeInsetsMake(0, 0, 0, margin);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        } break;
        case YYButtonContentLayoutStyleRightImageRight:{
            titleEdge = UIEdgeInsetsMake(0, -self.frame.size.width / 2, 0, imageWith + padding + margin);
            imageEdge = UIEdgeInsetsMake(0, 0, 0, -labelWidth + margin);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        } break;
        default:break;
    }
    self.imageEdgeInsets = imageEdge;
    self.titleEdgeInsets = titleEdge;
}

#pragma mark - associated

- (BOOL)yyLayoutFlag {
    return [objc_getAssociatedObject(self, @selector(yyLayoutFlag)) boolValue];
}

- (void)setYyLayoutFlag:(BOOL)yyLayoutFlag {
    objc_setAssociatedObject(self, @selector(yyLayoutFlag), @(yyLayoutFlag), OBJC_ASSOCIATION_ASSIGN);
}

- (NSInteger)layoutType{
    return [objc_getAssociatedObject(self, @selector(layoutType)) integerValue];
}

- (void)setLayoutType:(NSInteger)layoutType{
    objc_setAssociatedObject(self, @selector(layoutType), @(layoutType), OBJC_ASSOCIATION_ASSIGN);
    self.yyLayoutFlag = YES;
    [self setNeedsLayout];
}

- (CGFloat)padding{
    return [objc_getAssociatedObject(self, @selector(padding)) floatValue];
}

- (void)setPadding:(CGFloat)padding{
    objc_setAssociatedObject(self, @selector(padding), @(padding), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.yyLayoutFlag = YES;
    [self setNeedsLayout];
}
- (CGFloat)margin{
    return [objc_getAssociatedObject(self, @selector(margin)) floatValue];
}
- (void)setMargin:(CGFloat)margin{
    objc_setAssociatedObject(self, @selector(margin), @(margin), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    self.yyLayoutFlag = YES;
    [self setNeedsLayout];
}

@end
